﻿<cfcomponent output="false" extends="fileManager.util.Base">

	<cfset variables.templatePath = expandPath('/fileManager/util/codeGenerator/template/') />
	<cfset variables.remotePath = expandPath('#getDirectoryFromPath( cgi.script_name )#/remote/') />

	<cffunction name="init" access="public" returntype="CodeGenerator" output="false" >
		
		<cfset variables.Facade = readFile("#variables.templatePath#Facade.tmp") />
		<cfset variables.Method = readFile("#variables.templatePath#Method.tmp") />
		<cfset variables.Argument = readFile("#variables.templatePath#Argument.tmp") />
		
		<cfset variables.Proxy = readFile("#variables.templatePath#Proxy.tmp") />
		<cfset variables.Param = readFile("#variables.templatePath#Param.tmp") />
		
		<cfif not directoryExists( variables.remotePath )>
			<cfdirectory action="create" directory="#variables.remotePath#" />
		</cfif>
		
		<cfreturn this/>
	</cffunction>
	
	<cffunction name="createRemoteProxy" access="public" returntype="void" output="false" >
		<cfargument name="name" type="string" required="true" />
		<cfargument name="serviceName" type="string" required="true" />
		<cfargument name="methodList" type="array" required="true" />
		
		<cfset var func = 0 />
		<cfset var args = 0 />
		
		<cfset var myProxy = duplicate( variables.Proxy ) />
		<cfset var myParam = duplicate( variables.Param ) />
		
		<cfset var method = arguments.methodList[ 1 ] />
		<cfset var paramList = "" />
		
		
		<cfloop index="args" from="1" to="#arrayLen( arguments.methodList[ 1 ].parameters )#">
		
			<cfset myParam = duplicate( variables.Param ) />
			
			<cfset myParam = replace( myParam, "@@param-name@@", arguments.methodList[ 1 ].parameters[ args ].name, "all"  ) />
			<cfset myParam = replace( myParam, "@@param-value@@", arguments.methodList[ 1 ].parameters[ args ].value, "all"  ) />
		
			<cfset paramList = listAppend( paramList, myParam, chr(10) ) />
		
		</cfloop>
		
		<cfset myProxy = replace( myProxy, "@@service-name@@", arguments.serviceName, "all"  ) />
		<cfset myProxy = replace( myProxy, "@@method@@", arguments.methodList[ 1 ].name, "all"  ) />
		<cfset myProxy = replace( myProxy, "@@params@@", paramList, "all"  ) />
		
		<cfset writeFile( variables.remotePath & arguments.name & ".cfm", myProxy ) />
		
	</cffunction>

	
	<cffunction name="createRemoteFacade" access="public" returntype="void" output="false" >
		<cfargument name="target" type="string" required="true" />
		<cfargument name="name" type="string" required="true" />
		<cfargument name="serviceName" type="string" required="true" />
		<cfargument name="methodList" type="array" required="true" />
		
		<cfset var func = 0 />
		<cfset var args = 0 />
		
		<cfset var myFacade = duplicate( variables.Facade ) />
		<cfset var myMethod = duplicate( variables.Method ) />
		<cfset var myArgument = duplicate( variables.Argument ) />
		
		<cfset var functionList = "" />
		<cfset var argumentList = "" />
		
		<cfset var myTarget = getComponentMetadata( arguments.target ) />
		
		<cfloop index="func" from="1" to="#arrayLen( myTarget.functions )#">
		
			<cfset myMethod = duplicate( variables.Method ) />
			<cfset argumentList = "" />

			<cfif arguments.methodList.contains( myTarget.functions[ func ].name ) >
			
				<cfloop index="args" from="1" to="#arrayLen( myTarget.functions[ func ].parameters )#">
				
					<cfset myArgument = duplicate( variables.Argument ) />
					
					<cfset myArgument = replace( myArgument, "@@param@@", myTarget.functions[ func ].parameters[ args ].name, "all"  ) />
					<cfset myArgument = replace( myArgument, "@@type@@", myTarget.functions[ func ].parameters[ args ].type, "all"  ) />
				
					<cfset argumentList = listAppend( argumentList, myArgument, chr(10) ) />
				
				</cfloop>
				
				<cfset myMethod = replace( myMethod, "@@method@@", myTarget.functions[ func ].name, "all"  ) />
				<cfset myMethod = replace( myMethod, "@@argumentList@@", argumentList, "all"  ) />
				
				<cfset functionList = listAppend( functionList, myMethod, chr(10) ) />
			
			</cfif>
			
		</cfloop>
		
		<cfset myFacade = replace( myFacade, "@@service-name@@", arguments.serviceName, "all"  ) />
		<cfset myFacade = replace( myFacade, "@@methodList@@", functionList, "all"  ) />
		
		<cfset writeFile( variables.remotePath & arguments.name & ".cfc", myfacade ) />
		
	</cffunction>

</cfcomponent>